/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * 
 * @author yaoyuming
 * 
 * @date 2019-12-04 17:26
 */
package com.yao.eureka.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import com.yao.eureka.entity.ServerEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yaoyuming
 * @date 2019/12/04 17:26
 */
@RestController
@RequestMapping(value = "/home")
public class HomeController {

    @Value("${spring.application.name:unknow}")
    private String serverName;

    @Value("${server.port:unknow}")
    private String port;


    @PostMapping(value = "/post/server", produces = "application/json; charset=UTF-8")
    public ServerEntity postServer(@RequestParam("name") String name) {
        Console.log("postServer 传入的参数: {}", name);
        return new ServerEntity().setServerName(serverName).setPort(port);
    }

    @PostMapping(value = "/post/serverBody", produces = "application/json; charset=UTF-8")
    public ServerEntity postServer(@RequestBody ServerEntity server) {
        Console.log("postServer 传入的参数: {}", server);
        return new ServerEntity().setServerName(serverName).setPort(port);
    }


    @GetMapping(value="/get/server")
    public ServerEntity getServer(@RequestParam("name") String name, @RequestHeader("access_token") String token){
        Console.log("getServer 传入的参数: {} ;; token: {}", name, token);
        return new ServerEntity().setServerName(serverName).setIp(IdUtil.randomUUID()).setPort(port);
    }

    @GetMapping(value = "/list")
    public List<String> list(@RequestParam("ids") List<Integer> ids) {
        Console.log("ids = {}", ids);
        return ids.stream().filter(e -> null != e).map(e -> e.toString()).collect(Collectors.toList());
    }

}